/**
 * @file
 * @author LiQun
 * @date 2021/11/28
 */
const path = require('path');
const fs = require('fs-extra');

module.exports.getPort = function () {
  const infoPath = path.resolve(__dirname, '../cache/config.json');
  let exists = fs.existsSync(infoPath);
  if (exists) {
    let data = fs.readFileSync(infoPath, 'utf8');
    const {ideFilePath} = JSON.parse(data);
    const port = fs.readFileSync(ideFilePath, 'utf8');
    console.log('使用端口号:', port);
    return port;
  } else {
    throw new Error('未获取到配置信息!请重新进行初始化!')
  }
}


/**
 * 获取有效项目
 * @author LiQun
 * @date 2021/11/28
 */
function getActiveProjectInfo() {
  // 获取当前路径,判断是存在项目列表中
  const currentPath = path.resolve(__dirname);
  const projectInfoPath = path.resolve(__dirname, '../cache/project.json');
  let exists = fs.existsSync(projectInfoPath);
  let result = [];
  if (!exists) {
    return null;
  } else {
    const data = fs.readFileSync(projectInfoPath);
    result = JSON.parse(data);
    for (let i = 0; i < result.length; i++) {
      const projectInfo = result[i];
      if (projectInfo.projectPath === currentPath) {
        return projectInfo;
      }
    }
  }
  return result[0];
}

module.exports.getActiveProjectInfo = getActiveProjectInfo;
